<html>
<head><title>Calculator</title></head>
<body>
<h1>Calculator</h1>
by: Gabriel Lopez<br />
<br />
<form method="GET">
<input type="text" name="expr"><input type="submit" value="Calculate">
</form>
<?php
  $expr = $_GET["expr"];

  preg_match("#(\ *(\-|\+)?[0-9]+(\.[0-9]*)?)((\ *(\*|/)(\ *(\-|\+)?[0-9]+(\.[0-9]*)?))|(\ *(\-|\+)(\ +(\-|\+))?[0-9]+(\.[0-9]*)?))*\ *#", $expr, $matches);

  /* 
  echo "expr: " . $expr."<br />";
  echo "strlen: " . strlen($matches) . "<br />";
  for($i = 0; $i < strlen($matches); $i++){
    echo $i . ": " . $matches[$i] . "<br />";
  }
  */

  if($expr == "") {
    echo "";
  }
  else if($expr != $matches[0]){
    echo "<h2>Result</h2>";
    echo "Invalid input expression " . $expr . ".";
  }
  else {
    preg_match("#\.*/\ *[0]+(\-|\+|/|\*|\ |$|(\.[0]*(\-|\+|/|\*|\ |$)))\.*#", $expr, $zero);
    $ans = "";
    if($zero[0] == ""){
      eval("\$ans = $expr ;");
    }
    echo "<h2>Result</h2>";
    echo $expr . " = " . $ans;
  }
?>

</body>
</html>
